<html><head>
    <script src="../../../../../openlayers/lib/OpenLayers.js"></script>
    <script src="../../../../../ext/adapter/ext/ext-base.js"></script>
    <script src="../../../../../ext/ext-all.js"></script>
    <script src="../../../../lib/GeoExt.js"></script>

    <script>

    function test_constructor(t) {
        t.plan(5);
        
        var legend;

        // no config (won't render)
        legend = new GeoExt.VectorLegend();
        t.ok(legend instanceof GeoExt.VectorLegend, "instance of VectorLegend");
        t.ok(legend instanceof GeoExt.LayerLegend, "instance of LayerLegend");
        legend.destroy();
        
        // rules & symbolType
        legend = new GeoExt.VectorLegend({
            legendTitle: "my title",
            renderTo: "legendpanel",
            rules: [
                new OpenLayers.Rule({title: "first rule"}),
                new OpenLayers.Rule({title: "second rule"})
            ],
            symbolType: "Point"
        });
        
        // check title
        var titleCmp = legend.getComponent(0);
        t.eq(titleCmp.getEl().dom.innerHTML, "my title", "correct legend title");
        
        // check rule components    
        t.eq(legend.rulesContainer.items.length, 2, "two items in the rules container");
        var ruleCmp = legend.rulesContainer.getComponent(0);
        var ruleTitleCmp = ruleCmp.getComponent(1);
        // this should eventually become a container instead of a panel
        t.ok(ruleTitleCmp.getEl().dom.innerHTML.indexOf("first rule") > -1, "correct title for first rule");

        legend.destroy();
    }
    
    function test_setCurrentScaleDenominator(t) {
        t.plan(12);
        
        var legend = new GeoExt.VectorLegend({
            legendTitle: "my title",
            renderTo: "legendpanel",
            rules: [
                new OpenLayers.Rule({title: "first rule", maxScaleDenominator: 100}),
                new OpenLayers.Rule({title: "second rule", minScaleDenominator: 100})
            ],
            symbolType: "Point"
        });
        
        // all rules shown if no scale denominator is set
        t.eq(legend.rulesContainer.items.length, 2, "two items in the rules container");
        t.eq(legend.rulesContainer.getComponent(0).hidden, false, "first rule shown");
        t.eq(legend.rulesContainer.getComponent(1).hidden, false, "second rule shown");
        
        // show first rule at 1:50
        legend.setCurrentScaleDenominator(50)
        t.eq(legend.rulesContainer.items.length, 2, "two items in the rules container at 1:50");
        t.eq(legend.rulesContainer.getComponent(0).hidden, false, "first rule shown at 1:50");
        t.eq(legend.rulesContainer.getComponent(1).hidden, true, "second rule hidden at 1:50");

        // show second rule at 1:150
        legend.setCurrentScaleDenominator(150)
        t.eq(legend.rulesContainer.items.length, 2, "two items in the rules container at 1:150");
        t.eq(legend.rulesContainer.getComponent(0).hidden, true, "first rule hidden at 1:150");
        t.eq(legend.rulesContainer.getComponent(1).hidden, false, "second rule shown at 1:150");
        
        // show second rule at 1:100 (min is inclusive)
        legend.setCurrentScaleDenominator(100)
        t.eq(legend.rulesContainer.items.length, 2, "two items in the rules container at 1:100");
        t.eq(legend.rulesContainer.getComponent(0).hidden, true, "first rule hidden at 1:100");
        t.eq(legend.rulesContainer.getComponent(1).hidden, false, "second rule shown at 1:100");
        
        legend.destroy();
        
    }
    
    function test_update(t) {
        
        t.plan(2);

        var map = new OpenLayers.Map();
        var layer = new OpenLayers.Layer.Vector("old");
        map.addLayer(layer);

        var store = new GeoExt.data.LayerStore({map: map});
        
        var legend = new GeoExt.VectorLegend({
            layerRecord: store.getAt(0),
            renderTo: "legendpanel",
            symbolType: "Point"
        });
        
        // check title
        t.eq(legend.getComponent(0).getEl().dom.innerHTML, "old", "correct legend title");

        // change layer title
        layer.setName("new");

        // check title (only works if layer is added to a map)
        t.eq(legend.getComponent(0).getEl().dom.innerHTML, "new", "correct new legend title");

        legend.destroy();
        map.destroy();
    }
    

    </script>
</head><body>
    <div id="legendpanel"></div>
</body></html>
